//
//  SearchController.swift
//  appstore
//
//  Created by 王雷 on 2019/11/2.
//  Copyright © 2019 Roy. All rights reserved.
//

import UIKit

class SearchController: BaseViewController,UITableViewDataSource,UITableViewDelegate,UISearchControllerDelegate,UISearchResultsUpdating {
    
    let SearchTableViewCellIdentifier = "SearchTableViewCellIdentifier"
    
    let data = [
        "Hot Search",
        "Daily life",
        "League of Legends",
        "Wechat",
        "Game of Thrones",
        "Hupu JRS",
        "Game Center",
        "QQ Music"
    ]
    
    lazy var searchTableView = UITableView()
    
    lazy var searchController = UISearchController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.createView()
    }
    
    func createView() -> Void {
        self.navigationController?.navigationBar.prefersLargeTitles = true // 导航栏大标题
        super.changeNavigationBarBackground()
        self.navigationItem.title = "Search";
        self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
        
        self.searchController = UISearchController(searchResultsController: nil)
        self.searchController.delegate = self
        self.searchController.searchResultsUpdater = self
        self.searchController.obscuresBackgroundDuringPresentation = true // 搜索时背景变模糊
        self.searchController.hidesNavigationBarDuringPresentation = true
        self.searchController.searchBar.placeholder = "App Store";
        self.navigationItem.searchController = searchController
        self.navigationItem.hidesSearchBarWhenScrolling = false
        // 设置definesPresentationContext为true，我们保证在UISearchController在激活状态下用户push到下一个view controller之后search bar不会仍留在界面上
        definesPresentationContext = true
        
        self.searchTableView = UITableView(frame: CGRect.zero, style: .plain)
        self.searchTableView.dataSource = self
        self.searchTableView.delegate = self
        self.searchTableView.tableFooterView = UIView(frame: CGRect.zero)
        self.view.addSubview(searchTableView)
        self.searchTableView.snp.makeConstraints { (make) in
            make.bottom.top.left.right.equalTo(self.view)
        }
        
        self.searchTableView.register(UITableViewCell.self, forCellReuseIdentifier: SearchTableViewCellIdentifier)
    }
    

    // UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.data.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: SearchTableViewCellIdentifier)
        cell?.textLabel?.text = self.data[indexPath.row]
        if indexPath.row != 0 {
            cell?.textLabel?.textColor = .blue
        }
        return cell!
    }
    
    // UISearchResultsUpdating
    func updateSearchResults(for searchController: UISearchController) {
        print("SearchController - updateSearchResults")
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
